<template>
    <van-address-edit 
        show-delete
        show-set-default
        :area-list="areaList"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="address"
        @save="save"
        @delete="del"
    />
</template>

<script setup>
    //引入地区数据
    import { areaList } from '@vant/area-data'

    let address = ref({})
    const {proxy} = getCurrentInstance()
    const action = ref(proxy.$route.query.action)
    onBeforeMount(async()=>{
        const result = await proxy.$GET({
            url:'/business/address',
            params:{id:proxy.$route.query.id}
        })
        address.value = {
            name: result.consignee,
            tel: result.mobile,
            addressDetail: result.address,
            areaCode: result.code,
            isDefault: result.status=='1',
        }
    })
    const path = ref({})
    if(action.value=='select'){
        path.value = {path:'/address' , query: {action: 'select',addressid:proxy.$route.query.addressid}}
    }else{
        path.value = {path:'/address'}
    }
    const save = async (values) => {
        const result = await proxy.$PUT({
            url:'/business/address',
            params:{id:proxy.$route.query.id},
            data: {values},
        })
        if(result.code==1){
            
            showSuccessToast({
                message: result.msg,
                duration: 1000,
                onClose: () => proxy.$router.push(path.value)
            })
        }else{
            showFailToast({
                message: result.msg,
                duration: 1000,
            })
        }
    }
    const del = async () => {
        const result = await proxy.$DEL({
            url:'/business/address',
            params:{id:proxy.$route.query.id}
        })
        if(result.code==1){
            showSuccessToast({
                message: result.msg,
                duration: 1000,
                onClose: () => proxy.$router.push(path.value)
            })
        }else{
            showFailToast({
                message: result.msg,
                duration: 1000,
            })
        }
    }
</script>